"""字段配置Repository"""
from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.field_config import FieldConfig
from app.repositories.base import BaseRepository


class FieldConfigRepository(BaseRepository[FieldConfig]):
    """字段配置数据访问层"""

    def __init__(self, db: Session):
        super().__init__(FieldConfig, db)

    def get_by_task_id(self, task_id: int) -> List[FieldConfig]:
        """根据任务ID获取字段配置列表"""
        return (
            self.db.query(FieldConfig)
            .filter(FieldConfig.task_id == task_id)
            .order_by(FieldConfig.display_order, FieldConfig.field_name)
            .all()
        )

    def get_by_category(
        self, task_id: int, category: str
    ) -> List[FieldConfig]:
        """根据任务ID和字段分类获取配置列表"""
        return (
            self.db.query(FieldConfig)
            .filter(
                FieldConfig.task_id == task_id,
                FieldConfig.field_category == category,
            )
            .order_by(FieldConfig.display_order, FieldConfig.field_name)
            .all()
        )

    def get_by_field_name(
        self, task_id: int, field_name: str
    ) -> Optional[FieldConfig]:
        """根据任务ID和字段名获取配置"""
        return (
            self.db.query(FieldConfig)
            .filter(
                FieldConfig.task_id == task_id,
                FieldConfig.field_name == field_name,
            )
            .first()
        )

